
//Extend jQuery Ajax
/*
            $.doAjax("url", data, function (msg) {
            //msg 返回结果
            },false,false);
            $.doAjax("url", null, function (msg) {
            //msg 返回结果
            },true);
*/
$.extend($, {
    //url-地址,data-数据,callback-回调函数,loading-加载提示,cache-是否缓存
    doAjax: function (url, data, callback, loading, cache) {
        var defaultSetting = {
            type: "POST",
            cache: false,
            url: url,
            data: data,
            beforeSend: function () { $.LoadingShow(); },
            complete: function () { $.LoadingHide(); },
            error: function (XMLHttpRequest, textStatus, errorThrown) { $.LoadingErroe(XMLHttpRequest, textStatus, errorThrown); },
            success: function (response) {
                if (response.indexOf("script type='text/javascript'") > 0) {
                    $("body").append(response);
                    $("#LoadingProgressBar").attr("num", "0");
                    $.LoadingHide();
                    return false;
                }
                if (typeof callback != 'undefined') {
                    callback.call(this, response);
                    $.SuccessDispose(defaultSetting);
                }
            }
        };
        if (loading !== undefined && loading == false) {
            defaultSetting["beforeSend"] = undefined;
            defaultSetting["complete"] = undefined;
        } else if (loading !== undefined && loading != false && loading != true && loading != "") {
            defaultSetting["beforeSend"] = function () { $.LoadingShow(loading); };
        }
        if (cache !== undefined && cache == true) {
            defaultSetting["cache"] = true;
        }
        $.ajax(defaultSetting);
    },
    doAjaxGet: function (url, data, callback) {
        var defaultSetting = {
            type: "GET",
            cache: false,
            url: url,
            data: data,
            beforeSend: function () { $.LoadingShow(); },
            complete: function () { $.LoadingHide(); },
            success: function (response) {
                if (typeof callback != 'undefined') {
                    callback.call(this, response);
                }
            }
        };
        if (loading !== undefined && loading == false) {
            defaultSetting["beforeSend"] = undefined;
            defaultSetting["complete"] = undefined;
        }
        if (cache !== undefined && cache == true) {
            defaultSetting["cache"] = true;
        }
        $.ajax(defaultSetting);
    },
    LoadingShow: function (message) {
        var strMsg = "正在加载，请稍等...";
        if (message !== undefined && message != "") {
            strMsg = message;
        }
        var ajaxbg = $("#LoadingBackground,#LoadingProgressBar");
        if (ajaxbg.html() == null) {
            $("body").append('<div id="LoadingBackground" class="LoadingBackground" ></div><div num="1" id="LoadingProgressBar" class="LoadingProgressBar" >' + strMsg + '</div>');
        } else {
            var num = Number($("#LoadingProgressBar").attr("num"));
            $("#LoadingProgressBar").html(strMsg).attr("num", num + 1);
        }
        $("#LoadingBackground,#LoadingProgressBar").show();
    },
    LoadingHide: function () {
        var num = Number($("#LoadingProgressBar").attr("num"));
        if (num > 1) {
            $("#LoadingProgressBar").attr("num", num - 1);
        } else {
            $("#LoadingBackground,#LoadingProgressBar").remove();
        }
    },
    SuccessDispose: function (data) {
        /*
        此方法用于 其他 JS 需要页面加载 完成后 处理的 功能。
        */
    },
    SetStorage: function (keyName, keyValue) {
        if (window.applicationCache) {
            localStorage[keyName] = keyValue
        }
    },
    GetStorage: function (keyName) {
        if (window.applicationCache) {
            var value = localStorage[keyName];
            if (value != undefined && value != null) {
                return value;
            } else {
                return "";
            }
        } else {
            return "";
        }
    },
    LoadAsyncScript: function (src, callback) {
        if (callback == undefined) {
            callback = function () { };
        }

        var scriptList = document.getElementsByTagName("script");
        var isHave = 0;
        for (var i = 0; i < scriptList.length; i++) {
            var url = scriptList[i].src;
            if (url.indexOf(src) > -1) {
                isHave = 1;
                scriptList[i];
            }
        }
        if (isHave == 0) {
            var head = document.getElementsByTagName("head")[0];
            var script = document.createElement("script");
            script.setAttribute("type", "text/javascript");
            script.setAttribute("src", src);
            script.setAttribute("async", true);
            script.setAttribute("defer", true);
            head.appendChild(script);

            if (script.readyState) {//ie  
                script.onreadystatechange = function () {
                    var state = this.readyState;
                    if (state === 'loaded' || state === 'complete') {
                        callback();
                    }
                }
            } else {//Others: Firefox, Safari, Chrome, and Opera   
                script.onload = function () {
                    callback();
                }
            }
        }
    },
    LogHelper: function (subName, message) {
        $.ajax({
            type: "POST",
            url: "/Function/LogAction.ashx?type=weblog",
            data: {"subname":subName,"message":message},
            success: function (msg) {

            }
        });
    }
});


$(window).keyup(function (e) {
    e = e || event;
    if (e.keyCode == 118) {
        window.parent.setmysuggestion();
    }
});






//阻止事件冒泡的通用函数  
function stopBubble(e) {
    // 如果传入了事件对象，那么就是非ie浏览器  
    if (e && e.stopPropagation) {
        //因此它支持W3C的stopPropagation()方法  
        e.stopPropagation();
    } else {
        //否则我们使用ie的方法来取消事件冒泡  
        window.event.cancelBubble = true;
    }
}


function VMiddleImg(obj, options) {
    var defaults = {
        "width": null,
        "height": null
    };
    var opts = $.extend({}, defaults, options);
    return $(obj).each(function () {
        var $this = $(this);
        $this.css({ "width": "auto", "height": "auto" });

        var src = $this.attr("src");

        var objHeight = $this.height(); //图片高度
        var objWidth = $this.width(); //图片宽度
        var parentHeight = opts.height || $this.parent().height(); //图片父容器高度
        var parentWidth = opts.width || $this.parent().width(); //图片父容器宽度


        var ratio = objHeight / objWidth;
        if (objHeight > parentHeight && objWidth > parentWidth) {

            var wb = objWidth / parentWidth;
            var hb = objHeight / parentHeight;

            if (wb > hb) {

                objHeight = (objHeight / hb);
                objWidth = (objWidth / hb);

            } else {
                objWidth = (objWidth / wb);
                objHeight = (objHeight / wb);
            }


        } else if (objHeight > parentHeight) {
            objWidth = (parentHeight / ratio);
            objHeight = (parentHeight);
        }


        $this.css({
            "left": (parentWidth - objWidth) / 2,
            "top": (parentHeight - objHeight) / 2,
            "width": objWidth + "px",
            "height": objHeight + "px",
            opacity: 1, position: "inherit"
        });
    });
}